//index.js
//获取应用实例
var app = getApp()

// 语言
var util = require('../../utils/util.js')
import event from '../../utils/event'

Page({
  data: {
    //语言 - begin
    language: '',
    //语言 - end
    addressList:[]
  },

  selectTap: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.urls + '/index/changedefault',
      header: app.getPostRequestHeader(),
      method: 'POST',
      data: {
        customer_id:util.getCustomerId(),
        address_id: id
      },
      success: (res) => {
        app.saveReponseHeader(res);
        wx.navigateBack({})
      }
    })
  },
  // 删除地址
  deleteAddess(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.urls + '/customer/address/remove',
            //url: app.siteInfo.url + app.siteInfo.subDomain + '/user/shipping-address/delete',
            header: app.getPostRequestHeader(),
            data: {
              bdmin_user_id:util.share_bdmin_user_id(),
              address_id: id
            },
            success: (res) => {
              if (res.data.code == 200) {
                that.initShippingAddress();
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  addAddess : function () {
    wx.navigateTo({
      url:"/pages/address-add/address-add"
    })
  },
  
  editAddess: function (e) {
    wx.navigateTo({
      url: "/pages/address-add/address-add?id=" + e.currentTarget.dataset.id + "&isdefault=" + e.currentTarget.dataset.isdefault
    })
  },
  // 语言 
  // 设置language变量（翻译Object）
  setLanguage() {
    var lang = wx.T.getLanguage()
    this.setData({
      language: lang,
      selectSize: lang.select_attribute
    });
  },
  onLoad: function () {
    var that = this;
    if (app.globalData.iphone == true) { that.setData({ iphone: 'iphone' }) }
    // 语言
    // 设置当前页面的language变量 - 每个页面都要有
    this.setLanguage();
    event.on("languageChanged", this, this.setLanguage); // (2)
    // 设置当前页面的language Index - 每个页面都要有
    wx.T.setLocaleByIndex(wx.T.langIndex);
    // 语言 - 结束

    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight-res.statusBarHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio;
        // 设置高度
        that.setData({
          height: height-250
        });
      }
    })
  },
  onShow : function () {
    this.initShippingAddress();
  },
  initShippingAddress: function () {
    var that = this;
    wx.request({
      url: app.globalData.urls +'/checkout/onepage/getaddresslist',
      header: app.getRequestHeader(),
      data: {
        bdmin_user_id:util.share_bdmin_user_id(), 
      },
      success: (res) =>{
        if (res.data.code == 200) {
          var addressColl = res.data.data.addressList
          var addressList = []
          for (var x in addressColl) {
            var addressOne = addressColl[x]
            var addressInfo = addressOne.address_info
            addressList.push({
              id: addressInfo.address_id,
              isDefault: addressOne.is_default == "1" ? true : false,
              linkMan: addressInfo.first_name + addressInfo.last_name,
              mobile: addressInfo.telephone,
              provinceStr: addressInfo.state,
              cityStr: addressInfo.city,
              areaStr: addressInfo.area,
              address: addressInfo.street1,
            })
          }
          that.setData({
            addressList: addressList,
            loadingMoreHidden: true
          });
        } else if (res.data.code == 700){
          that.setData({
            addressList: null,
            loadingMoreHidden: false
          });
        }
        app.saveReponseHeader(res);
      }
    })
  },
  // 删除地址
  deleteAddress: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.urls + '/customer/address/remove',
            //url: app.siteInfo.url + app.siteInfo.subDomain + '/user/shipping-address/delete',
            header: app.getPostRequestHeader(),
            data: {
              bdmin_user_id:util.share_bdmin_user_id(),
              address_id: id
            },
            success: (res) => {
              if (res.data.code == 200) {
                that.initShippingAddress();
              }
              app.saveReponseHeader(res);
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})
